package cn.com.nes.site.listener;

import cn.com.nes.site.entity.em.ShiFou;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class ShiFouConverter implements Converter<Integer> {
    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        for (ShiFou enumData : ShiFou.values()) {
            if (CellDataTypeEnum.STRING.equals(cellData.getType())) {
                if (enumData.getName().equals(cellData.getStringValue())) {
                    return enumData.getValue();
                } else if (enumData.getValue().toString().equals(cellData.getStringValue())) {
                    return enumData.getValue();
                }
            } else if (CellDataTypeEnum.NUMBER.equals(cellData.getType())) {
                if (enumData.getValue().equals(cellData.getNumberValue().intValue())) {
                    return enumData.getValue();
                }
            }
        }
        return 0;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        ShiFou enumData;
        try {
            enumData = 0 == value ? ShiFou.NO : ShiFou.YES;
        } catch (IllegalArgumentException e) {
            return new WriteCellData<>(value.toString());
        }
        return new WriteCellData<>(enumData.getName());
    }
}
